package com.lagou.edu.threadlocal;

import java.util.concurrent.*;

/**
 * @author 赵虎
 * @date 2021/1/11 3:12 上午
 */
public class ThreadPool {
    public static final int CORES_NUM = Runtime.getRuntime().availableProcessors();

    public static ThreadPoolExecutor getThreadPoolExecutor() {
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(CORES_NUM);
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
        return new ThreadPoolExecutor(CORES_NUM, CORES_NUM, 60, TimeUnit.SECONDS, workQueue, threadFactory, handler);
    }

    public static ScheduledThreadPoolExecutor  getScheduledThreadPoolExecutor() {
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
        return new ScheduledThreadPoolExecutor(CORES_NUM, threadFactory, handler);
    }

    public static ThreadPoolExecutor getThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, int workQueueSize) {
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(workQueueSize);
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
        return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, workQueue, threadFactory, handler);
    }

}
